#!/bin/bash
#************************************************#
#         автор: kuzyara555@gmail.com            #
#                Ноябрь  , 2012                  #
#                                                #
#      Скачиваем 20 фильмов XTO                  #
#            с наиб. кол-м сидов                 #
#************************************************#

TR_HOST="x-torrents.org"
TR_USER="Гаечка"
TR_PASSWORD="pirate"
DIR="/media/D/FILMS" #fullpath

SC_COOKIE="/tmp/gt-$TR_HOST-$TR_USER.ck"
SC_UA="Mozilla/5.0 (X11; U; Linux x86_64; ru; rv:1.9.2.15) Gecko/20110303 Ubuntu/10.10 (maverick) Firefox/3.6.15"


if [ -w $SC_COOKIE ]; then
	echo "---Cookie exists. Cheking auth"
	auth_page="`curl -b $SC_COOKIE  -c $SC_COOKIE -A "$SC_UA" $TR_HOST`"
#	echo "$auth_page" > auth_page.html
	result=`echo "$auth_page" | grep -o --max-count=1 "$TR_USER"`
fi
echo "---Username: $result"
sleep 1

if [ -z "$result" ]; then
    echo "---Cookie wrong. Makin auth";
    auth_path="http://x-torrents.org/takelogin.php";
    if [ -w $SC_COOKIE ]; then
	cookie_data=`cat $SC_COOKIE`
	curl --trace-ascii - -b $SC_COOKIE  -c $SC_COOKIE -A "$SC_UA" -d "username=$TR_USER" \
		-d "password=$TR_PASSWORD" -d "returnto=/" "$auth_path">/dev/null
    else
	curl --trace-ascii - -c $SC_COOKIE -A "$SC_UA" -d "username=$TR_USER" \
		-d "password=$TR_PASSWORD" -d "returnto=/" "$auth_path">/dev/null
    fi
fi
sleep 1

echo "---Authorised. Getting list_id"
tracker_page="`curl  -b $SC_COOKIE -c $SC_COOKIE -A "$SC_UA" "http://x-torrents.org/browse.php?search_method=0&search_in=0&cat=4&genre=0&incldead=0&sort=7&type=desc"`"
echo "$tracker_page" > /tmp/tracker_page.html
list_paths="`echo "$tracker_page" | \
	tr -d "\n" | iconv -t utf8 -f cp1251 | \
	grep -P -o '<td align="left" class="class10" >.*?</td>' | egrep -i "Проверена" | \
	grep -P -o '(?<=class10" ><a href=")[^"]*' | head -n 20`"
echo -e "---Pages_path:\n$list_paths";
if [ -z "$list_paths" ]; then
	echo "--Parsing error!"
	exit
fi


rm -f $DIR/*.aria2
rm -rf /tmp/*.torrent

echo "---Cycle begin"
cd /tmp/ #curl -O /tmp/
for path in $list_paths
do
echo "---Getting page: $path"
torrent_page="`curl -b $SC_COOKIE -A "$SC_UA" "$TR_HOST$path"`"
echo "$torrent_page" > /tmp/torrent_page.html
torrent_path="`echo "$torrent_page" | grep -P -o '(?<=left"><a class="index" href=").*?(?="><font color="#ce0000">)'`"
echo "---Getting .torrent file: $torrent_path"
curl -O -b $SC_COOKIE -A "$SC_UA" "$TR_HOST$torrent_path"
done

#loading 2GIS.Irkutsk
#curl -s irkutsk.2gis.ru | grep -P -o "http://download.2gis.ru/arhives/2GISShell.*?\.exe" | xargs wget -cP /mnt/ALL/\!Софт/
#find /mnt/ALL/\!Софт/2GIS* | sort | sed -n 2,+99p | xargs rm -f #delete old versions

echo "Delete old torrents"
cat /tmp/*.torrent | \
	grep -aPo "(?<=4:name)[0-9]+:.*?(?=[0-9]+:)" | \
	sed "s/\([0-9]*\):\(.*\)/\2/" > /tmp/newtorrentfiles.txt
ls -t1 "$DIR" > /tmp/torrentfiles.txt
grep -v --file=/tmp/newtorrentfiles.txt /tmp/torrentfiles.txt | \
	while read file; do echo rm -rf "$DIR/$file"; done;

#screen -d -m -L 
aria2c --no-conf --seed-time=0 --bt-max-peers=5 -V -j20 -d "$DIR" /tmp/*.torrent